<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="tree" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- :data指定要渲染的数据 -->
        <!-- :props指定数据中的对象的某个属性在树形组件显示子节点的属性名 -->
        <!-- @node-click:点击事件,点击某一个节点,获取节点对象数据 -->
        <el-tree :data="list">
          <!-- 使用插件在el-tree内容自定义内容 -->
          <!-- 在template上使用默认作用于插槽 #default={data} 获取足尖上的数据进行渲染 -->
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="kucha" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children && data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!--

         -->
      </el-card>
      <el-dialog

        title="新增子部门"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        @close="hClose"
      >
        <!-- 组件
        解决数据不跟新得方法
        v-if 销毁和重新创建 只是有点影响性能
        v-if="showDialog" ref="myDept"-->
        <addOrEdit
          :id="curId"
          ref="myDept"
          :title="isEdit?'编辑部门':'新增子部门'"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog = false"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getDepartments, deleteDepartments } from '@/api/department'
import { tranListToTreeList } from '@/utils'
import addOrEdit from './deptDialog.vue'
export default {
  components: {
    addOrEdit
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      showDialog: false,
      curId: '', // 父级的ID
      isEdit: false,
      originList: []

    }
  },
  async created() {
    this.loadDepartment()
  },
  methods: {
    async loadDepartment() {
      try {
        const res = await getDepartments()
        console.log(res.data.depts)
        this.originList = res.data.depts.map(item => {
          const { name, code, id, pid } = item
          return { name, code, id, pid }
        })
        res.data.depts.shift() // 删除第一条数据
        // 将res.data.depts 的平铺数据转换成 树状数据 导入 tranListToTreeList方法
        this.list = tranListToTreeList(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    hAdd(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true // 将编辑的状态改为true
      this.showDialog = true
      // 在父组件调用子组件的方法
      this.$nextTick(() => {
        this.$refs.myDept.loadDepartmentsDetail()
      })
    },
    hSuccess() {
      // 关闭弹层,重新刷新页面
      this.showDialog = false
      this.loadDepartment()
    },
    hDel(id) {
      this.$confirm('你确认要删除该部门吗，是否继续？', '提示', {
        distinguishCancelAndClose: true,
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await deleteDepartments(id)
          console.log(res, 'delpartment')
          if (res.success) {
            // 重新刷新页面
            this.loadDepartment()
          }
        })
        .catch(action => {
          this.$message({
            type: 'info',
            message: '取消删除'
          })
        })
    },
    hClose() {
      console.log('关闭弹层')
      // 清空子组件 的表单
      this.$refs.myDept.resetForm()
    }
  }
}
</script>
